package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增菜品
     * @param setmealDTO
     */
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        //先要获取新增的菜品
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);

        //上诉把套餐给插入到数据库当中，根据分类的id，再查询分类下的菜品
        Long setmealId = setmeal.getId();//获取的是套餐的id
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();//获取的是套餐下的菜品,初始时应该是空
        setmealDishes.forEach(setmealDish -> {//套餐里面可以添加多种菜品,将每一个菜品的SetmealId，设置为setmealId
            setmealDish.setSetmealId(setmealId);//
        });

        setmealDishMapper.insert(setmealDishes);//为套餐插入相应的菜品
    }

    /**
     * 套餐的分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult selectByPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> setmealVOS= setmealMapper.selectBypage(setmealPageQueryDTO);
        long total = setmealVOS.getTotal();
        List<SetmealVO> result = setmealVOS.getResult();
        PageResult pageResult = new PageResult(total,result);
        return pageResult;
    }

    /**
     * 删除套餐
     * @param ids
     */


    @Transactional
    public void deleteSetmeal(List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getByid(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                //起售中的套餐不能够进行删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            } else {
                setmealMapper.deleteBySetmeal(id);//删除套餐
                //删除套餐下的菜品
                setmealDishMapper.deldetByDish(id);
            }
        }


    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    public SetmealVO getById(Long id) {
        Setmeal setmeal = setmealMapper.getByid(id);
        //根据套餐的id查询套餐下的菜品
        List<SetmealDish> dishes= setmealDishMapper.getwithdish(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(dishes);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    public void updateBysetmeal(SetmealDTO setmealDTO) {
        //修改套餐内的基本信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updateSetmeal(setmeal);
        //根据id删除套餐内的基本菜品
        setmealDishMapper.deldetByDish(setmealDTO.getId());
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //在套餐中重新添加新的菜品
        if (setmealDishes!=null&&setmealDishes.size()>0){
            setmealDishes.forEach(setmealdish -> {
                setmealdish.setSetmealId(setmealDTO.getId());
            });
            setmealDishMapper.insertdish(setmealDishes);

        }

    }

    /**
     * 起售禁售商品
     * @param status
     * @param id
     */
    public void startorend(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        setmealMapper.updateSetmeal(setmeal);

    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }


    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }






}
